package com.micro.modual.service.impl;

import com.micro.common.MicroResponseCode;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.modual.entity.GoodsParameters;
import com.micro.modual.entity.GoodsTemplate;
import com.micro.modual.extension.GoodsParametersExtension;
import com.micro.modual.mapper.GoodsParametersMapper;
import com.micro.modual.mapper.GoodsTemplateMapper;
import com.micro.modual.model.bo.goodsparameters.GoodsParametersBO;
import com.micro.modual.model.dto.goodsparameters.GoodsParametersExistBO;
import com.micro.modual.service.GoodsParametersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
*  <h1>商品参数表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("goodsParametersService")
public class GoodsParametersServiceImpl extends AbstractBasicsServiceImpl<GoodsParameters,Long> implements GoodsParametersService {

    @Autowired
    private GoodsParametersMapper goodsParametersMapper;

    @Autowired
    private GoodsTemplateMapper goodsTemplateMapper;

    /**
     * <h2>新增商品参数表详情</h2>
     * @param goodsParametersBO {@link GoodsParametersBO} 商品参数表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addGoodsParameters(GoodsParametersBO goodsParametersBO) {
        GoodsParameters goodsParameters = GoodsParametersExtension.converAddGoodsParameters(goodsParametersBO);
        goodsParametersMapper.insertSelective(goodsParameters);
        GoodsTemplate goodsTemplate = goodsTemplateMapper.findTemplateByCode(goodsParameters.getTemplateCode());
        goodsTemplate.setParametersCount(goodsTemplate.getParametersCount() + 1);
        goodsTemplateMapper.updateByPrimaryKeySelective(goodsTemplate);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改商品参数表详情</h2>
     * @param goodsParametersBO  {@link GoodsParametersBO} 商品参数表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editGoodsParameters(GoodsParametersBO goodsParametersBO) {
        GoodsParameters goodsParameters = GoodsParametersExtension.converEditGoodsParameters(goodsParametersBO);
        goodsParametersMapper.updateByPrimaryKeySelective(goodsParameters);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除商品参数表信息</h2>
     * @param id  商品参数表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteGoodsParameters(Long id) {
        GoodsParameters goodsParameters = goodsParametersMapper.selectByPrimaryKey(id);
        GoodsTemplate goodsTemplate = goodsTemplateMapper.findTemplateByCode(goodsParameters.getTemplateCode());
        goodsTemplate.setParametersCount(goodsTemplate.getParametersCount() -1);
        goodsTemplateMapper.updateByPrimaryKeySelective(goodsTemplate);
        goodsParametersMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJson validataParamsNameExist(GoodsParametersExistBO goodsParametersExistBO) {
        if(goodsParametersMapper.findParamsExist(goodsParametersExistBO.getParamsName(),goodsParametersExistBO.getTemplateCode()) > 0 ){
            throw new BizException(MicroResponseCode.GOODS_PARAMS_HAS_EXIST.getResponseCode(), MicroResponseCode.GOODS_PARAMS_HAS_EXIST.getResponseMessage());
        }else{
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

}
